using Avalonia;
using Avalonia.Controls;

namespace AnimationPage.AttachedProperties;

internal sealed class FocusAndSelectProperty : AnimateBaseProperty<FocusAndSelectProperty>
{
    public static readonly AvaloniaProperty<bool> ValueProperty =
        AvaloniaProperty.RegisterAttached<AvaloniaObject, bool>
            ("Value", typeof(FocusAndSelectProperty), coerce: OnCallBack);

    private static bool OnCallBack(AvaloniaObject arg1, bool arg2) => OnCallBack(ValueProperty, arg1, arg2);

    public static bool GetValue(AvaloniaObject d) => d.GetValue<bool>(ValueProperty);

    public static void SetValue(AvaloniaObject d, bool value) => d.SetValue(ValueProperty, value);

    protected override void OnValueChanged(AvaloniaProperty<bool> property, AvaloniaObject sender, bool value)
    {
        if (sender is not TextBox textBox || !value)
            return;
        
        textBox.Focus();
        textBox.SelectAll();
    }
}